import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/utils/store.js';

//注册全局组件  router-link   router-view
Vue.use(VueRouter)


// 1. 定义 (路由) 组件。
// 可以从其他文件 import 进来
// @--src

// 2. 定义路由
const routes = [{
		name: 'Home',
		path: '/home',
		redirect: '/'
	},
	{
		name: 'Home',
		path: '/',
		component: () => import('@/views/Home.vue'),
		children: [
			{
				name: 'User',
				path: '/user',
				component:() => import('@/views/User.vue'),
			},
			{
				path: '/role',
				component: () => import('@/views/Role.vue'),
			},
			{
				path: '/menu1',
				component:  () => import('@/views/menu/v1'),
			},{
				path: '/menu2',
				component:  () => import('@/views/menu/v2'),
			},
			{
				path: '/dept',
				component: () => import('@/views/dept'),
			},{
				path: '/vuex',
				component: () => import('@/views/vuex'),
			},
			{
				path: '/news',
				component:  () => import('@/views/news'),
				children: [{
					path: '/news/local',
					component:  () => import('@/views/news/Local'),
				}, {
					path: '/news/internation',
					component:  () => import('@/views/news/Internation')
				}]
			}

		]
	},
	{
		name: 'Login',
		path: '/login',
		component: () => import('@/views/Login.vue')
	},
	{
		path: '*',
		component: () => import('@/views/NotFound.vue')
	},



]

// 3. 创建 router 实例，然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。

/**   
 * 
 
     Hash 
 *    http://127.0.0.1/#/user
 *    http://127.0.0.1/#/role
 *    http://127.0.0.1/#menu
 
     History
 *    http://127.0.0.1/user
 *    http://127.0.0.1/role
 *    http://127.0.0.1/menu
    
	
	VueRouter   --- Vue实例（根）
 */
const router = new VueRouter({
	// url 、
	// mode: 'history',
	mode: 'hash',
	routes // (缩写) 相当于 routes: routes
})


//全局的
router.beforeEach((to, from, next) => {
     // console.log("from",from);
     // console.log("to",to);
     // console.log("next",next);
	 
	 let toPath = to.path;
	 if(toPath === '/login'){
		 next()
	 }else{   //     /
		 //判断是否存在登录状态
		 
		 const username = store.state.username;
		 if(username){
			 //可以放行  
			 next();
		 }else{
			//没有登录去登录
			next("/login"); 
		 }
		 
		 
		
	 }
	
	 
	 
	 
})


export default router